package user;

import book.Book;
import book.Library;
import book.PairOfUidAndBookId;
import constant.Constant;
import utils.AnalyzingBorrowedBook;

import java.util.Scanner;

public class NormalUser extends User{

    //借阅图书的信息->文件来的
    private PairOfUidAndBookId[] pairOfUidAndBookIds;
    // 当前书籍的借阅量
    private int borrowedCount;

    //最多借阅的图书数量
    private static final int BORROW_BOOK_MAX_NUM = 5;

    private final AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();


    Scanner scanner = new Scanner(System.in);

    private Library library;

    public NormalUser(String name, int userID) {
        super(name, userID,"普通用户");
        library = Library.getLibrary();
        //不管你是哪个用户，只要你实例化之后，会加载所有用户的借阅信息
        loadBorrowedBook();
    }


    private void loadBorrowedBook() {

        PairOfUidAndBookId[] allBorrowedBook;
        //1.先加载文件当中的借阅信息
        allBorrowedBook= analyzingBorrowedBook.loadObject(Constant.BORROWED_BOOK_FILE_NAME);
        //2. 默认已借阅的图书数组大小为BORROW_BOOK_MAX_NUM，这里也可以定义到常量类
        pairOfUidAndBookIds = new PairOfUidAndBookId[BORROW_BOOK_MAX_NUM];
        //3.没有读取到已借阅的图书信息
        if (allBorrowedBook== null) {
            borrowedCount = 0;
        } else {
            //4. 查看实际读取到的数组长度是多少？
            int allBorrowedBookLen= allBorrowedBook.length;
            //5. 如果读取到了10本书被借阅 但是当前borrowedBooks数组长度小于10
            if (allBorrowedBookLen> pairOfUidAndBookIds.length) {
                //6. 按照实际情况进行分配数组内存
                pairOfUidAndBookIds = new PairOfUidAndBookId[allBorrowedBookLen];
            }
            //7.把数据拷贝回到 已借阅图书信息的数组当中
            for (int i = 0; i < allBorrowedBookLen; i++) {
                //if(信息.userID == this.userID)
                pairOfUidAndBookIds[i] = allBorrowedBook[i];
            }
            //8.更新当前实际借阅书籍的书籍数量
            borrowedCount = allBorrowedBookLen;
        }

    }

    private void storeBorrowedBook() {
        analyzingBorrowedBook.storeObject(pairOfUidAndBookIds,Constant.BORROWED_BOOK_FILE_NAME);
    }



    @Override
    public int display() {
        System.out.println("普通用户 " + name + " 的操作菜单:");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个人借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }


    public void borrowBook() {
        System.out.println("目前书籍如下：" );
        library.displyBook();
        System.out.println("请输入你要借阅的图书ID：");
        int bookId = scanner.nextInt();
        scanner.nextLine();

        //1. 检查如果没有书籍，就不能借阅
        if(library.getBookCount() == 0) {
            System.out.println("书架没有书籍暂且不能借阅");
            return;
        }

        //加载已借阅的图书信息
        loadBorrowedBook();

        Book book = library.searchById(bookId);
        if(book == null) {
            System.out.println("没有你要借阅的书籍....");
            return;
        }

        //检查 是否当前要借阅的书籍 之前被自己 或者 别人借阅过
        for (int i = 0; i < borrowedCount; i++) {
            PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
            //找到了对应的书籍
            if (pairOfUidAndBookId.getBookId() == book.getBookId()) {
                if (getUserID() == pairOfUidAndBookId.getUserId()) {
                    System.out.println("该书已经被你借阅过了，你的ID是：" + getUserID());
                    return;
                } else {
                    System.out.println("该书已经被其他人借阅过了，他的ID是：" + pairOfUidAndBookId.getUserId());
                    return;
                }
            }
        }

        //书籍信息
        library.borrowBook(bookId);

        //封装借阅信息的对象写到 借阅表当中
        PairOfUidAndBookId pairOfUidAndBookId = new PairOfUidAndBookId(userID, book.getBookId());
        pairOfUidAndBookIds[borrowedCount] = pairOfUidAndBookId;
        borrowedCount++;

        //存储借阅图书
        storeBorrowedBook();

        System.out.println("借阅成功！");


    }

    //归还图书
    public void returnBook() {

        System.out.println("目前书籍如下：" );
        library.displyBook();
        System.out.println("请输入你要归还的图书ID：");
        int bookId = scanner.nextInt();
        scanner.nextLine();

        //1. 检查如果没有书籍，就不能借阅
        if(library.getBookCount() == 0) {
            System.out.println("书架没有书籍暂且不能归还");
            return;
        }

        //加载已借阅的图书信息
        loadBorrowedBook();

        Book book = library.searchById(bookId);
        if(book == null) {
            System.out.println("没有你要归还的书籍....");
            return;
        }


        for (int i = 0; i < borrowedCount; i++) {
            //如果2本书的ID，则认为是同一本书
            if (pairOfUidAndBookIds[i].getBookId()==book.getBookId()) {
                //借阅用户也一样
                if (getUserID() == pairOfUidAndBookIds[i].getUserId()) {

                    library.returnBook(bookId);

                    System.out.println("图书 '" + book.getTitle() + "' 已成功归还。");
                    // 用最后一本替换归还的书
                    pairOfUidAndBookIds[i] = pairOfUidAndBookIds[borrowedCount - 1];
                    // 清空最后一个
                    pairOfUidAndBookIds[borrowedCount - 1] = null;
                    borrowedCount--;
                    storeBorrowedBook();
                }else{
                    System.out.println("该书籍不是你借阅的书籍，不能归还："+book.getTitle());
                }
                return;
            }
        }
        System.out.println("你没有借阅该书籍，不需要归还，书籍ID为："+bookId);

    }

    // 查看个人借阅情况
    public void viewBorrowHistory() {
        //读取当前借阅所有用户的借阅书籍的情况
        loadBorrowedBook();
        System.out.println("您的借阅情况如下：");
        if (borrowedCount == 0) {
            System.out.println("目前没有借阅记录.....");
        } else {
            boolean flg = false;
            for (int i = 0; i < borrowedCount; i++) {
                //这里只能查看属于自己借阅的情况
                //用户ID相同的情况下，使用书籍ID查询书籍
                if(pairOfUidAndBookIds[i].getUserId() == userID) {
                    flg = true;
                    Book book = library.searchById(pairOfUidAndBookIds[i].getBookId());
                    System.out.println(book);
                }
            }
            if(!flg) {
                System.out.println("你没有借阅过书籍！");
            }
        }
    }

}
